﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BoKeYuanApp.BLL
{
    public class HttpHelp
    {
        public string WebClientGet(string uri)
        {
            WebClient wc = new WebClient();
            Stream stream = wc.OpenRead(uri);
            string htmlStr = string.Empty;
            using (StreamReader sr = new StreamReader(stream))
            {
                htmlStr = sr.ReadToEnd();
            }

            return htmlStr;
        }
        public string HttpClientGet(string url)
        {
            string httpUrl1 = url;
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = httpClient.GetAsync(new Uri(httpUrl1)).Result;
            string retString = response.Content.ReadAsStringAsync().Result;

            var resultStr = Regex.Matches(retString, @"(?is)<body[^>]*?>([\s\S].*?)</body>")
                .Cast<Match>().Select(mx => mx.Groups[0].Value.TrimStart().TrimEnd()).ToList();
            return retString;
        }
        public string HttpWebRequestGet(string url)
        {
            string httpUrl1 = url;
            HttpWebRequest req =
                (HttpWebRequest)HttpWebRequest.Create(httpUrl1);
            req.Method = "GET";
            string htmlStr = string.Empty;
            using (WebResponse wr = req.GetResponse())
            {
                //在这里对接收到的页面内容进行处理
                using (Stream responseStream = wr.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
                    {
                        htmlStr = reader.ReadToEnd();
                    }
                }
            }

            return htmlStr;
        }
    }
}
